package com.esig.Model;

import com.esig.View.GameFrame;
import com.esig.util.Direction;
import com.esig.util.State;

import java.awt.*;
import java.awt.event.KeyEvent;

import static com.esig.util.Constant.REPAINT_INTERVAL;

/**
 * 主角之一  Davis
 */
public class PlayerOne extends Role implements Runnable {

    private boolean up = false;
    private boolean left = false;
    private boolean right = false;
    private boolean down = false;


    //构造方法 带精灵图截取
    public PlayerOne(String img,  GameFrame gameFrame, String leftImg_0, String leftImg_1, String leftImg_2, String rightImg_0, String rightImg_1, String rightImg_2) {
        super(img, gameFrame, leftImg_0, leftImg_1, leftImg_2, rightImg_0, rightImg_1, rightImg_2);
    }

    //构造方法
    public PlayerOne(String img, int dx1, int dy1, GameFrame gameFrame,  String leftImg_0, String leftImg_1, String leftImg_2, String rightImg_0, String rightImg_1, String rightImg_2) {
        super(img, dx1, dy1, gameFrame, leftImg_0, leftImg_1, leftImg_2, rightImg_0, rightImg_1, rightImg_2);
    }


    @Override
    public void paintSelf(Graphics g) {
        /**
         * 绘制自身
         * 然后就是截取的范围（左上和右下两个点）精灵图一般一格是80*80
         *
         * 其中img是Image图片对象，而d开头的都是destinetiong，s开头的是source。
         * 也就是说在这里，你分别定义好来源和目标的两个矩形的左上角和右下角的点，它会自动帮你剪裁和适应。
         * 当然为了保证不失真，最好宽度和高度相同。
         */
        g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
        move();
    }


    /**
     * 设置矩形大小去实现物品碰撞等
     *
     * @return
     */
    @Override
    public Rectangle getRec() {
        //这里dx1和dy1是目标（图）上的左上角，宽度右右下角的x减左上角的x，y同理
        return new Rectangle(dx1, dy1, dx2 - dx1, dy2 - dy1); //设置了矩形但是图片大小没有改变
    }

    /**
     * 按下事件
     *
     * @param keyCode
     */
    public void keyPressed(int keyCode) {
        switch (keyCode) {
            //上下左右移动
            case KeyEvent.VK_A:
                keyPA();
                break;
            case KeyEvent.VK_S:
                down = true;
                break;
            case KeyEvent.VK_D:
                keyPD();
                break;
            case KeyEvent.VK_W:
                up = true;
                break;
            case KeyEvent.VK_J:
                keyJ();
                break;
            case KeyEvent.VK_K:
                jump();
                break;
            case KeyEvent.VK_L:
                defense();
                break;
            case KeyEvent.VK_U:
                killOneOne();
                break;
            case KeyEvent.VK_I:
                keyI();
                break;
            default:
                break;
        }
    }

    /**
     * 松开事件
     *
     * @param keyCode
     */
    public void keyReleased(int keyCode) {
        switch (keyCode) {
            case KeyEvent.VK_A:
                keyRA();
                break;
            case KeyEvent.VK_S:
                down = false;
                moveStop();
                break;
            case KeyEvent.VK_D:
                keyRD();
                break;
            case KeyEvent.VK_W:
                up = false;
                moveStop();
                break;
            default:
                break;
        }
    }

    public void move() {
        if (left) {
            leftWalk();
        } else if (right) {
            rightWalk();
        } else if (up) {
            upWalk();
        } else if (down) {
            downWalk();
        }
    }

    //按键变量
    public int keyCode = 0;

    //按键j的方法
    public int jTimes = 1;  //攻击段数
    public long jStopTime = 0;   //收拳时间 判断x ms内，没有再次按下平a就收拳
    public long jPressTime = 0;  //按下时间

    public void keyJ() {
        keyCode = KeyEvent.VK_J;
        if (jTimes == 1) {
            jStopTime = 0;
            if (state == State.STAND || state == State.WALK) {
                new Thread(this).start();
                jPressTime = System.currentTimeMillis();
                attackOne();
                jTimes++;
            } else if (state == State.ATTACK_THREE && System.currentTimeMillis() - jPressTime >= 250) {
                jPressTime = System.currentTimeMillis();
                attackOne();
                jTimes++;
            } else if (state == State.RUN) {
                attackRun();
            } else if (state == State.JUMP) {
                attackJump();
            }

        } else if (jTimes == 2 && System.currentTimeMillis() - jPressTime >= 250) {
            jStopTime = 0;
            jPressTime = System.currentTimeMillis();
            attackTwo();
            jTimes++;
        } else if (jTimes == 3 && System.currentTimeMillis() - jPressTime >= 750) {
            jTimes = 1;
            jStopTime = 0;
            jPressTime = System.currentTimeMillis();
            attackThree();
        }

    }

    public long iStopTime = 0;   //收技能时间 判断x ms内，没有再次按下平i就收拳
    public boolean iOneReady = true; //判断是否第一段i键准备,否则是第二段i
    public long iPressTime = 0;  //i键按下时间

    public void keyI() {
        keyCode = KeyEvent.VK_I;
        if (state == State.STAND || state == State.WALK || state == State.RUN || state == State.SKILL_TWO_ONE || state == State.SKILL_TWO_TWO) {
            //第一次按i键
            if (iPressTime == 0) {
                new Thread(this).start();
                killTwoOne();
                iOneReady = false;
                bulletLike();
            }
            //第一段i技能
            if (iOneReady && System.currentTimeMillis() - iPressTime > 500 && iPressTime != 0) {
                killTwoOne();
                iOneReady = false;
                bulletLike();
            }
            //第二段i技能
            if (!iOneReady && System.currentTimeMillis() - iPressTime > 500 && iPressTime != 0) {
                killTwoTwo();
                iOneReady = true;
                bulletLike();
            }
        }

    }

    //技能二 气功波相同之处
    public void bulletLike() {
        iStopTime = 0;
        iPressTime = System.currentTimeMillis();
        //生成子弹对象并添加子弹到子弹列表
        if (direction == Direction.RIGHT) {
            Bullet bullet = new Bullet("src/main/resources/img/davis_ball.png", dx1 + 34, dy1 + 18, dx2 + 34, dy1 + 45 + 18, 0, 0, 80, 45, direction, gameFrame);
            gameFrame.bulletList.add(bullet);
        } else {
            Bullet bullet = new Bullet("src/main/resources/img/davis_ball_mirror.png", dx1 - 50, dy1 + 18, dx1 + 30, dy1 + 45 + 18, 240, 0, 320, 45, direction, gameFrame);
            gameFrame.bulletList.add(bullet);
        }
    }

    public boolean aRunReady = false;   //a键跑步状态是否就绪
    public long aReleaseTime = 0;  //a键松开时间

    public boolean dRunReady = false;   //d键跑步状态是否就绪
    public long dReleaseTime = 0;  //d键松开时间

    public void keyPA() {
        left = true;

        if (aRunReady) {
            if (System.currentTimeMillis() - aReleaseTime <= 200) {
                state = State.RUN;
                System.out.println("跑步");
            }
        } else {
            aRunReady = false;
            moveStop();
        }
    }

    public void keyPD() {
        right = true;
        if (dRunReady) {
            if (System.currentTimeMillis() - dReleaseTime <= 200) {
                state = State.RUN;
                System.out.println("跑步");
            }
        } else {
            dRunReady = false;
            moveStop();
        }
    }

    public void keyRA() {
        //松开的时候获取线程，200ms内再次按下激活跑步准备
        left = false;
        aReleaseTime = System.currentTimeMillis();
        aRunReady = true;   //跑步状态

        moveStop();
    }

    public void keyRD() {
        //松开的时候获取线程，200ms内再次按下激活跑步准备
        right = false;

        dReleaseTime = System.currentTimeMillis();
        dRunReady = true;   //跑步状态
        moveStop();
    }


    @Override
    public void run() {
        while (true) {
            //j按键线程
            if (keyCode == KeyEvent.VK_J) {
                jStopTime += 100;
                if (jStopTime >= 1000) {
                    jStopTime = 0;
                    jTimes = 1;
                    moveStop();
                    return;
                }

            }

            if (keyCode == KeyEvent.VK_I) {
                iStopTime += 100;
                if (iStopTime >= 750) {
                    iPressTime = 0;
                    iStopTime = 0;
                    iOneReady = true;
                    moveStop();
                    return;
                }
            }

            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    public void jump() {
        //设置跳跃状态
        state = State.JUMP;

        sy1 = 480;
        sy2 = 560;

        if (direction == Direction.RIGHT) {
            changeTimerParameter = "右跳跃";
            sx1 = 160;
            sx2 = 240;
        } else {
            changeTimerParameter = "左跳跃";
            sx1 = 560;
            sx2 = 640;
        }
        changeOnStart();
        imgOnStart();

    }

    public void defense() {
        imgOnEnd();
        changeOnEnd();
    }

    //这个移动停止后的动作，目前代表了所有操作后的停止动作
    public void moveStop() {
//        if (state == State.STAND || state == State.WALK || state == State.ATTACK_ONE || state == State.ATTACK_TWO || state == State.ATTACK_THREE || state == State.RUN) {
       if(state != State.ATTACK_RUN){
           state = State.STAND;
           sy1 = 0;
           sy2 = 80;
           if (direction == Direction.RIGHT) {
               setImg(leftImg_0);
               sx1 = 240;
               sx2 = 320;
           } else {
               setImg(rightImg_0);
               sx1 = 480;
               sx2 = 560;
           }
           imgOnEnd();
       }
    }

//    public void attackStop() {
//        if (state == State.ATTACK_ONE || state == State.ATTACK_TWO || state == State.ATTACK_THREE) {
//            moveStop();
//        }
//    }


}
